package com.fly.demo;

import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.Socket;

public class Main3Activity extends AppCompatActivity {

    TextView textView, textView1 ;

    private String text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main3);

        Button button = (Button) findViewById(R.id.main3_btn);
        textView = (TextView) findViewById(R.id.main3_text1);
        textView1 = (TextView) findViewById(R.id.main3_text2);
        Button button1 = (Button) findViewById(R.id.main3_btn2);

        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {
                            Socket socket = new Socket("120.27.218.48", 66);
                            OutputStream outputStream = socket.getOutputStream();
                            outputStream.write("tcp演示,1111".getBytes());
                            //读取客户端返回的数据
                            InputStream inputStream = socket.getInputStream();
                            ByteArrayOutputStream baos = new ByteArrayOutputStream();
                            byte[] buf = new byte[1024];
                            int length = inputStream.read(buf);
                            text = new String(buf, 0, length);
                            while (true) {
                                if (socket.isConnected()) {
                                    if (!socket.isInputShutdown()) {
                                        if ((length = inputStream.read(buf)) != -1) {
                                            baos.write(buf, 0, length);
                                        }
                                        text = new String(baos.toByteArray());
                                        Log.d("TAG", "------TEXT----" + text);
                                        Main3Activity.this.runOnUiThread(new Runnable() {
                                            @Override
                                            public void run() {
                                                textView1.setText(text);
                                            }
                                        });
                                    }
                                }
                            }
                         //   socket.close();

                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }).start();

            }
        });

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                textView1.setText(wifiInfo.getSSID());
            }
        });
    }
}
